﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Student
    {
        private int stno;
        private string name;
        private int age;
        public int Stno
        {
            get { return this.stno; }
            set { this.stno = value; }
        }
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
        public int Age
        {
            get { return this.age; }
            set { this.age = value; }
        }
        public Student(int stno, string name, int age)
        {
            Stno = stno;
            Name = name;
            Age = age;
        }
        public override string ToString()
        {
            return $"学号：{Stno}  姓名：{Name} 年龄：{Age}  ";
        }

    }
    class StnoSort : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            //if (x.Stno > y.Stno)
            //{
            //    return 1;
            //}
            //else
            //{
            //    return -1;
            //}
            return x.Stno.CompareTo(y.Stno);
        }
    }
    class NameSort : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            //if (x.Name > y.Name)
            //{
            //    return 1;
            //}
            //else
            //{
            //    return -1;
            //}
            return x.Name.CompareTo(y.Name);
        }
    }
    class AgeSort : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            //if (x.Age > y.Age)
            //{
            //    return 1;
            //}
            //else
            //{
            //    return -1;
            //}
            return x.Age.CompareTo(y.Age);
        }
    }
    class Program
    {
        //添加3个类，分别实现 IComparer<T>接口，实现对Student类的三个字段的排序。
        //1、学生类：学号、姓名、年龄 
        //2、请选择：1、添加学生信息。2、查询学生信息。
        //3、重复的学号不能添加。
        //4、查询学生信息功能中有：
        //1、查询所有（按学号排序）2、查询所有（按姓名排序），3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出
        static void Main(string[] args)
        {
            List<Student> list = new List<Student>();
            //ArrayList list1 = new ArrayList();
            Hashtable list1 = new Hashtable();
            //已有学员
            Student st = new Student(34,"鑫哥",19);
            Student st1 = new Student(14, "索隆", 30);
            Student st2 = new Student(24, "凯", 25);
            list.Add(st);
            list.Add(st1);
            list.Add(st2);
            list1.Add(st.Stno,st);
            list1.Add(st1.Stno, st1);
            list1.Add(st2.Stno, st2);
                 login:
            Console.WriteLine("请选择：1、添加学生信息2、查询学生信息");
            int arr = int.Parse(Console.ReadLine());
            string flag = null;
            switch (arr)
            {
                case 1:
                    
                  while (true)
                    {
                    Console.WriteLine("请添加学生学号：");
                    int a = int.Parse(Console.ReadLine());
                    Console.WriteLine("请添加学生姓名：");
                    string b = Console.ReadLine();
                    Console.WriteLine("请添加学生年龄：");
                    int c = int.Parse(Console.ReadLine());
                    Student st3 = new Student(a, b, c);
                        if (list1.Contains(a))
                        {
                            Console.WriteLine("重复重新输入");
                            goto login;
                        }
                        else
                        {
                        list1.Add(st3.Stno, st3);
                        }
                        Console.WriteLine("要继续吗？(y/n)");
                        flag = Console.ReadLine();
                        if (flag.Equals("n"))
                        {
                            foreach (var t in list1.Keys)
                            {
                                Console.WriteLine(list1[t]);
                            }
                            break;
                        }
                    }
                   

                    break;
                case 2:
                    Console.WriteLine("请选择你要查询的学生信息1、查询所有（按学号排序）2、查询所有（按姓名排序），3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出：");
                    int outing = int.Parse(Console.ReadLine());
                    switch (outing)
                    {
                        case 1:
                            foreach (Student s in list)
                            {
                                Console.WriteLine(s);
                            }

                            list.Sort(new StnoSort());
                            Console.WriteLine("====================");
                            foreach (Student s in list)
                            {
                                Console.WriteLine(s);
                            }
                            break;
                        case 2:
                            foreach (Student s in list)
                            {
                                Console.WriteLine(s);
                            }

                            list.Sort(new NameSort());
                            Console.WriteLine("====================");
                            foreach (Student s in list)
                            {
                                Console.WriteLine(s);
                            }
                            break;
                        case 3:
                            foreach (Student s in list)
                            {
                                Console.WriteLine(s);
                            }

                            list.Sort(new AgeSort());
                            Console.WriteLine("====================");
                            foreach (Student s in list)
                            {
                                Console.WriteLine(s);
                            }
                            break;
                        case 4:
                            Console.WriteLine("请输入你要查的学号：");
                            int find = int.Parse(Console.ReadLine());
                                if (list1.Contains(find))
                                {
                                    Console.WriteLine(list1[find]);
                                }
                                else
                                {
                                    Console.WriteLine("查无此学生!!!");
                                }
                            break;
                        case 5:
                            Console.WriteLine("正在退出中···");
                            break;
                        default:
                            Console.WriteLine("sb没有该选项，能不能长点脑子？！！");
                            break;
                    }
                    break;
                default:
                    Console.WriteLine("sb没有该选项，能不能长点脑子？！！");
                    break;
            }
            Console.ReadKey();
        }
    }
}
